<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-10 23:59:20 +0200 (Thu, 10 Jul 2008) $
 * $Rev: 1285 $
 * $Author: gavinspearhead $
 * $Id: ajax_showstatus.php 1285 2008-07-10 21:59:20Z gavinspearhead $
 */
if (!defined('ORIGINAL_PAGE')) die('This file cannot be accessed directly.');


function match_iafd_link($line)
{
	$rv = preg_match('/(http\:\/\/.*iafd\.com\/[\w?\-\/?+%-=]*)/i', $line, $matches);
	if ($rv) {
		return trim($matches[1]);
	} else
		return FALSE;
}


function match_imdb_link($line)
{
	$rv = preg_match('/(http\:\/\/.*imdb\.com\/[\w?\-\/?]*)/i', $line, $matches);
	if ($rv) {
		return trim($matches[1]);
	} else
		return FALSE;
}


function match_tvrage_link($line)
{
	$rv = preg_match('/(http\:\/\/.*\.tvrage\.com\/\w*)/i', $line, $matches);
	if ($rv) {
		return trim($matches[1]);
	} else
		return FALSE;
}


function match_tvcom_link($line)
{
	$rv = preg_match('/(http\:\/\/.*\.tv\.com\/[\w.\/\-]*)/i', $line, $matches);
	if ($rv) {
		return trim($matches[1]);
	} else
		return FALSE;
}


function match_title($line)
{
	$rv = preg_match('/(\btitle\b|file\s?name\b|\benglish name\b|\bname\b)[\s:\-.\[]*((([\w.:\']+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2]) && !stristr($line, 'subtitle') && !stristr($line, 'imdb.com')) {
		$match = str_ireplace('.avi', '', $matches[2]);
		return trim($match);
	} else
		return FALSE;
}


function match_serietitle($line)
{
	$rv = preg_match('/(\bserie\b)[\s:\-.\[]*((([\w.:\']+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		$match = str_ireplace('.avi', '', $matches[2]);
		return trim($match);
	} else
		return FALSE;
}


function match_subtitles($line)
{
	$rv = preg_match('/(\bsubs\b|\bsubtitles\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2]) && !stristr($matches[2], 'vobsub')) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_genre($line)
{
	$rv = preg_match('/(\bgenre\b|\bcategory\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_language($line)
{
	$rv = preg_match('/(\blanguage\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_year($line)
{
	$rv = preg_match('/(\byear\b)[\s:\-.\[]*(\d{2,4})/i', $line, $matches);
	if ($rv && isset($matches[2]) && is_numeric($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_albumtitle($line)
{
	$rv = preg_match('/(\balbum\b|\btitle\b|\btitel\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_band($line)
{
	$rv = preg_match('/(\bband\b|\bartists?\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_software_os($line)
{
	$rv = preg_match('/(\bos type\b|\boperating systems?\b|\bos\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset( $matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_software_format($line)
{
	$rv = preg_match('/(\bformat\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_software_genre($line)
{
	$rv = preg_match('/(\bsoftware type\b|\bsoftware genre\b|\bsoftware category\b|\bgame type\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		if (stristr($matches[1], 'game'))
			$matches[2] = 'Game: ' . $matches[2];
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_format($line)
{
	$rv = preg_match('/(\bformat\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function match_softwaretitle($line)
{
	$rv = preg_match('/(\bsoftware name\b|\btitle\b|\btitel\b|\bname\b)[\s:\-.\[]*((([\w]+)[\s,|\/]*)+)/i', $line, $matches);
	if ($rv && isset($matches[2])) {
		return trim($matches[2]);
	} else
		return FALSE;
}


function set_info (array &$file_nfo, $index, $value)
{
	if ((!isset($file_nfo[$index]) || strlen($file_nfo[$index]) < 2) && strlen($value) > 2) {
		$file_nfo[$index] = trim($value);
		$pos = strpos($file_nfo[$index], '   ');
		if  ($pos > 10)
			$file_nfo[$index] = trim(substr($file_nfo[$index], 0 , $pos));
	}
}


function parse_tvcom_info($movie_link)
{
	$f = fopen($movie_link, 'r');
	if ($f === FALSE) return FALSE;
	$got_title = $got_rating = $got_year = FALSE;
	$rv = array ('name'=>NULL, 'rating'=>NULL, 'year'=>NULL);
	while (($line = fgets($f)) !== FALSE) {
		if (!$got_title && preg_match('/<title>(.*)at TV.com<\/title>/i', $line, $matches)) {
			$name = html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
			str_replace('"', '', $name);
			$rv['name'] = utf8_decode($name);
			$got_title = TRUE;
		} elseif (!$got_rating && preg_match('/<span class="number">([0-9]{1,2})\.?(\d)?<\/span>/i', $line, $matches)) {
			$decimals = $matches[2];
			if ($decimals >=3 && $decimals <=7) $decimals = 5; else $decimals = 0;
			$rv['rating'] = $matches[1]. ".$decimals";
			$got_rating = TRUE;
		} elseif (!$got_year && preg_match('/<p>\w+day\s+\w+\s+\d+,\s+([0-9]{4})<\/p>/i', $line, $matches)) {
			$rv['year'] = $matches[1];
			$got_year = TRUE;
		} elseif (!$got_year && preg_match('/<div class="airdate">Aired:\s+\d+\/\d+\/(\d+)\s*<\/div>/i', $line, $matches)) {
			$year = $matches[1];
			$got_year = TRUE;
			if ($year < 30)
				$year += 2000;
			else if ($year < 100)
				$year += 1900;
			$rv['year'] = $year;
		}
		if ($got_rating && $got_title && $got_year) {
			break;
		}
	}
	fclose($f);
	return $rv;
}


function parse_tvrage_info($movie_link)
{
	$f = fopen($movie_link, 'r');
	if ($f === FALSE) return FALSE;
	$got_title = $got_rating = FALSE;
	$rv = array ('name'=>NULL, 'rating'=>NULL);
	while (($line = fgets($f)) !== FALSE) {
		if (!$got_title && preg_match('/<h3[\w\s\'\"\=]*>(.*)\s\(\d+\s*Fans\).*<\/h3>/i', $line, $matches)) {
			$name = html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
			str_replace('"', '', $name);
			$rv['name'] = utf8_decode($name);
			$got_title = TRUE;
		} elseif (!$got_rating && preg_match('/(\d{1,2})\.(\d)\/10/i', $line, $matches)) {
			$decimals = $matches[2];
			if ($decimals >= 3 && $decimals <= 7) $decimals = 5; else $decimals = 0;
			$rv['rating'] = $matches[1]. ".$decimals";
			$got_rating = TRUE;
		}
		if ($got_rating && $got_title)
			break;
	}
	fclose($f);
	return $rv;
}


function parse_imdb_info($movie_link)
{
	$f = fopen($movie_link, 'r');
	if ($f === FALSE) return FALSE;
	$got_title = $got_rating = $got_genre = $next_is_genre =  FALSE;
	$rv = array ('name'=>NULL, 'year'=>NULL, 'rating'=>NULL, 'genre'=>NULL);
	while (($line = fgets($f)) !== FALSE) {
		if (!$got_title && preg_match('/<title>(.*)\((\d{4}).*\).*<\/title>/i', $line, $matches)) {
			$name = html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
			str_replace('"', '', $name);
			$rv['name'] = utf8_decode($name);
			$rv['year'] = $matches[2];
			$got_title = TRUE;
		} elseif (!$got_rating && preg_match('/(\d{1,2})\.(\d)\/10/i', $line, $matches)) {
			$decimals = (int)$matches[2];
			$value = (int)$matches[1];
			if ($decimals >= 3 && $decimals <= 7) {
				$decimals = 5; 
			} else {
				if ($decimals > 7) 
					$value += 1;
				$decimals = 0;
			} 
			$rv['rating'] = "$value.$decimals";
			$got_rating = TRUE;
		}elseif (!$got_genre && preg_match('/<h5>Genre:<\/h5>/i', $line)) {
			$next_is_genre = TRUE;
		} elseif ($next_is_genre && !$got_genre) {
			$rv['genre'] = trim(str_replace(' more', '', strip_tags($line))) ;
			$next_is_genre = FALSE;
			$got_genre = TRUE;
		}
		if ($got_rating && $got_title && $got_genre)
			break;
	}
	fclose($f);
	return $rv;
}


function find_info($str, $key)
{
	$file_nfo = array();
	$fn = array();
	switch ($key) {
	case SETTYPE_MOVIE:
		$fn[] = array('match_imdb_link', 'link');
		$fn[] = array('match_iafd_link', 'link');
		$fn[] = array('match_genre', 'moviegenre');
		$fn[] = array('match_title', 'name');
		$fn[] = array('match_subtitles', 'sublang');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_title', 'year');
		break;
	case SETTYPE_TVSERIES:
		$fn[] = array('match_imdb_link', 'link');
		$fn[] = array('match_tvrage_link', 'link');
		$fn[] = array('match_tvcom_link', 'link');
		$fn[] = array('match_genre', 'moviegenre');
		$fn[] = array('match_serietitle', 'name');
		$fn[] = array('match_title', 'name');
		$fn[] = array('match_subtitles', 'sublang');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_title', 'year');
		break;
	case SETTYPE_ALBUM:
		$fn[] = array('match_genre', 'musicgenre');
		$fn[] = array('match_albumtitle', 'name');
		$fn[] = array('match_year', 'year');
		$fn[] = array('match_band', 'band');
		break;
	case SETTYPE_SOFTWARE:
		$fn[] = array('match_softwaretitle', 'name');
		$fn[] = array('match_software_os', 'os');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_software_format', 'softwareformat');
		$fn[] = array('match_software_genre', 'softwaregenre');
		break;
	case SETTYPE_EBOOK: // usually no nfo files :(
		$fn[] = array('match_format', 'ebookformat');
		break;
	case SETTYPE_GAME:
		$fn[] = array('match_softwaretitle', 'name');
		$fn[] = array('match_software_os', 'os');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_software_format', 'gameformat');
		$fn[] = array('match_software_genre', 'gamegenre');
		break;
	case SETTYPE_DOCUMENTARY:
		$fn[] = array('match_genre', 'moviegenre');
		$fn[] = array('match_title', 'name');
		$fn[] = array('match_subtitles', 'sublang');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_title', 'year');
		break;
	case SETTYPE_TVSHOW:
		$fn[] = array('match_serietitle', 'name');
		$fn[] = array('match_title', 'name');
		$fn[] = array('match_subtitles', 'sublang');
		$fn[] = array('match_language', 'lang');
		$fn[] = array('match_title', 'year');
		break;

	case SETTYPE_IMAGE:
	default:
		break;
	}

	foreach($str as $line) {
		foreach($fn as $f)
			set_info ($file_nfo, $f[1], $f[0]($line));
	}
	get_link_info($file_nfo);

	if ($key == SETTYPE_ALBUM) {
		if (isset($file_nfo['name']) && isset($file_nfo['band']))
			$file_nfo['name'] = $file_nfo['band'] . ' - ' . $file_nfo['name'];
		else if (isset($file_nfo['band']))
			$file_nfo['name'] = $file_nfo['band'];
	}
	return $file_nfo;
}


function get_link_info(array &$file_nfo)
{
	if (isset($file_nfo['link']) && strpos($file_nfo['link'], 'imdb.com') !== FALSE) {
		$rv = parse_imdb_info($file_nfo['link']);
		if ($rv['name'] !== NULL)
			$file_nfo['name'] = $rv['name'];
		if ($rv['year'] !== NULL)
			$file_nfo['year'] = $rv['year'];
		if ($rv['rating'] !== NULL) 
			$file_nfo['score'] = $rv['rating'];
		if ($rv['genre'] !== NULL) 
			$file_nfo['moviegenre'] = $rv['genre'];
	}
	if (isset($file_nfo['link']) && strpos($file_nfo['link'], 'tvrage.com') !== FALSE) {
		$rv = parse_tvrage_info($file_nfo['link']);
		if ($rv['name'] !== NULL)
			$file_nfo['name'] = $rv['name'];
		if ($rv['rating'] !== NULL) 
			$file_nfo['score'] = $rv['rating'];
	}
	if (isset($file_nfo['link']) && strpos($file_nfo['link'], 'tv.com') !== FALSE) {
		$rv = parse_tvcom_info($file_nfo['link']);
		if ($rv['name'] !== NULL)
			$file_nfo['name'] = $rv['name'];
		if ($rv['rating'] !== NULL) 
			$file_nfo['score'] = $rv['rating'];
		if ($rv['year'] !== NULL) 
			$file_nfo['year'] = $rv['year'];
	}
}


function parse_nfo_file($filename, $key)
{
	$contents = file($filename);
	return find_info($contents, $key);
}


?>
